package org.jeecg.modules.demo.mes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.demo.mes.constant.StationMessage;
import org.jeecg.modules.demo.mes.controller.query.ProductionProcessQuery;
import org.jeecg.modules.demo.mes.entity.MesBaseFactory;
import org.jeecg.modules.demo.mes.entity.MesWorkOrder;
import org.jeecg.modules.demo.mes.entity.MesWorkOrderDetail;
import org.jeecg.modules.demo.mes.service.IMesProductionProcessService;
import org.jeecg.modules.demo.mes.service.IMesWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产过程控制类
 */
@RestController
@Api(tags = "生产过程")
@Slf4j
@RequestMapping("mes/productionProcess")
public class MesProductionProcessController {

    @Autowired
    private IMesProductionProcessService mesProductionProcessService;
    @Autowired
    private StationMessage stationMessage;
    @Autowired
    private IMesWorkOrderService mesWorkOrderService;

    /**
     * 初始化生产过程
     */
    @ApiOperation(value = "初始化生产过程", notes = "初始化生产过程")
    @GetMapping("initProductionProcess")
    public Result<Map<String, Object>> initProductionProcess() {
        checkStationMessageEmpty(stationMessage);
        final Map<String, Object> result = new HashMap<>();
        final MesBaseFactory factory = mesProductionProcessService.queryFactoryMsgByField("factory");
        result.put("factory", factory);
        final MesBaseFactory workshop = mesProductionProcessService.queryFactoryMsgByField("workshop");
        result.put("workshop", workshop);
        final MesBaseFactory line = mesProductionProcessService.queryFactoryMsgByField("line");
        result.put("line", line);
        final MesBaseFactory station = mesProductionProcessService.queryFactoryMsgByField("station");
        result.put("station", station);
        return Result.ok(result);
    }

    /**
     * 校验配置项是否配置完全
     */
    private void checkStationMessageEmpty(StationMessage stationMessage) {
        final String factoryName = stationMessage.getFactoryName();
        if (StringUtils.isBlank(factoryName)) {
            throw new JeecgBootException("配置文件中缺少工厂信息，请补充后重启后台项目再重新访问");
        }
        final String workshopName = stationMessage.getWorkshopName();
        if (StringUtils.isBlank(workshopName)) {
            throw new JeecgBootException("配置文件中缺少车间信息，请补充后重启后台项目再重新访问");
        }
        final String lineName = stationMessage.getLineName();
        if (StringUtils.isBlank(lineName)) {
            throw new JeecgBootException("配置文件中缺少产线信息，请补充后重启后台项目再重新访问");
        }
        final String stationName = stationMessage.getStationName();
        if (StringUtils.isBlank(stationName)) {
            throw new JeecgBootException("配置文件中缺少工位信息，请补充后重启后台项目再重新访问");
        }
    }

    /**
     * 通过工单条码获取工单信息
     */
    @ApiOperation(value = "通过工单号获取工单", notes = "通过工单号获取工单")
    @GetMapping(value = "queryWorkOrderByWorkOrderBarCode")
    public Result<Object> queryWorkOrderByWorkOrderBarCode(@RequestParam(value = "workOrderBarCode", required = true) String workOrderBarCode) {
        //将本道工序的信息返回到前端
        return Result.ok(mesProductionProcessService.queryWorkOrderByWorkOrderBarCode(workOrderBarCode));
    }

    /**
     * 校验物料条码是否正确
     */
    @ApiOperation(value = "校验物料条码是否正确", notes = "校验物料条码是否正确")
    @GetMapping("checkMaterialBarCode")
    public Result<Object> checkMaterialBarCode(@RequestParam("materialBarCode") String materialBarCode) {
        return Result.ok(mesProductionProcessService.queryMaterialDetailByBarCode(materialBarCode));
    }

    /**
     * 工单物料操作完成
     */
    @ApiOperation(value = "工单物料操作完成", notes = "工单物料操作完成")
    @PostMapping("finishMaterial")
    public Result finishMaterial(@RequestBody Map<String,Object> map) {
        final String procedureId = (String) map.get("procedureId");
        if (StringUtils.isBlank(procedureId)){
            throw new JeecgBootException("工序id为空");
        }
        final String workOrderBarCode = (String) map.get("workOrderBarCode");
        if (StringUtils.isBlank(workOrderBarCode)){
            throw new JeecgBootException("工单条码为空");
        }
        final List<Map<String,Object>> list = (List<Map<String,Object>>) map.get("list");
        mesProductionProcessService.finishMaterial(procedureId, workOrderBarCode, list);
        return Result.ok();
    }

    /**
     * 工单检测操作完成
     */
    @ApiOperation(value = "工单检测操作完成", notes = "工单检测操作完成")
    @PostMapping("finishCheck")
    public Result finishCheck(@RequestBody Map<String,Object> map) {
        final String procedureId = (String) map.get("procedureId");
        if (StringUtils.isBlank(procedureId)){
            throw new JeecgBootException("工序id为空");
        }
        final String workOrderBarCode = (String) map.get("workOrderBarCode");
        if (StringUtils.isBlank(workOrderBarCode)){
            throw new JeecgBootException("工单条码为空");
        }
        final List<Map<String,Object>> list = (List<Map<String,Object>>) map.get("list");
        mesProductionProcessService.finishCheck(procedureId, workOrderBarCode, list);
        return Result.ok();
    }

    @ApiOperation(value = "获取本工序的工单详情列表", notes = "获取本工序的工单详情列表")
    @GetMapping("queryProcedureWorkOrderList")
    public Result queryProcedureWorkOrderList(ProductionProcessQuery query) {
        final Map<String,Object> cxMap = new HashMap<>();
        cxMap.put("factoryId",query.getFactoryId());
        cxMap.put("workshopId",query.getWorkshopId());
        cxMap.put("lineId",query.getLineId());
        cxMap.put("workOrderStatus","2");
        if (StringUtils.equals("1",query.getFinishStatus())) {
            //待完成列表中查询的工单应该是未完成的
            cxMap.put("finishStatus", "1");
        }
        final List<MesWorkOrder> mesWorkOrders = mesWorkOrderService.queryTechnologyListSelective(cxMap);
        if (mesWorkOrders==null || mesWorkOrders.size()==0){
            return Result.ok(new Page<>());
        }
        final MesWorkOrder mesWorkOrder = mesWorkOrders.get(0);
        query.setWorkOrderId(mesWorkOrder.getId());
        final List<MesWorkOrderDetail> list = mesProductionProcessService.queryProcedureWorkOrderList(query);
        final Integer total = mesProductionProcessService.queryProcedureWorkOrderCount(query);
        IPage<MesWorkOrderDetail> pageList = new Page<>();
        pageList.setCurrent(query.getPageNo());
        pageList.setTotal(total);
        pageList.setRecords(list);
        return Result.ok(pageList);
    }

}
